package com.ruoyi.safe.service.impl;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.safe.domain.BizProjectCheckRecord;
import com.ruoyi.safe.domain.dto.BizExcludePlanTaskDTO;
import com.ruoyi.safe.domain.dto.ProjectSearchDTO;
import com.ruoyi.safe.domain.vo.CommonProjectVO;
import com.ruoyi.safe.mapper.CommonProjectMapper;
import com.ruoyi.safe.service.IBizExcludePlanTaskService;
import com.ruoyi.safe.service.IBizProjectCheckRecordService;
import com.ruoyi.safe.service.IPlanService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LB
 * @create 2024/1/8
 */
@Service
@AllArgsConstructor
@Slf4j
public class PlanServiceImpl implements IPlanService {

    private final IBizProjectCheckRecordService bizProjectCheckRecordService;
    private final IBizExcludePlanTaskService bizExcludePlanTaskService;
    private final CommonProjectMapper commonProjectMapper;

    @Override
    @Transactional
    public void DangerTimeUpdate() {
        BizProjectCheckRecord queryRecord = new BizProjectCheckRecord();
        queryRecord.setHiddenDangerStatus("1");
        queryRecord.setOverdueRectification("0");
        List<BizProjectCheckRecord> bizProjectCheckRecords = bizProjectCheckRecordService.selectBizProjectCheckRecordList(queryRecord);
        bizProjectCheckRecords.forEach(record -> {
            if (StringUtils.isNotNull(record.getReviseTheDates()) && new Date().after(record.getReviseTheDates())) {
                record.setOverdueRectification("1");
            }
            record.setUpdateBy("admin");
            bizProjectCheckRecordService.updateBizProjectCheckRecord(record);
        });
        queryRecord = new BizProjectCheckRecord();
        queryRecord.setHiddenDangerStatus("2");
        queryRecord.setOverdueReview("0");
        List<BizProjectCheckRecord> bizProjectCheckRecords2 = bizProjectCheckRecordService.selectBizProjectCheckRecordList(queryRecord);
        bizProjectCheckRecords2.forEach(record -> {
            if (StringUtils.isNotNull(record.getReviewTime()) && new Date().after(record.getReviewTime())) {
                record.setOverdueReview("1");
            }
            record.setUpdateBy("admin");
            bizProjectCheckRecordService.updateBizProjectCheckRecord(record);
        });
    }

    @Override
    public void  planTask() {
        List<SysDictData> list_root_type = DictUtils.getDictCache("list_root_type");
        List<CommonProjectVO> commonProjectVOS = commonProjectMapper.selectCommonProjectList(new ProjectSearchDTO());
        for (CommonProjectVO commonProjectVO : commonProjectVOS) {
            for (SysDictData sysDictData : list_root_type) {
                BizExcludePlanTaskDTO bizExcludePlanTaskDTO = new BizExcludePlanTaskDTO();
                bizExcludePlanTaskDTO.setProjectId(commonProjectVO.getId());
//                bizExcludePlanTaskDTO.setListType(sysDictData.getDictValue());
//                bizExcludePlanTaskDTO.setTableName(sysDictData.getRemark());
                bizExcludePlanTaskDTO.setAutoType("自动生成任务");
                Map<String, Object> map = bizExcludePlanTaskService.insertBizExcludePlanTaskBatch(bizExcludePlanTaskDTO);
                log.info("===== 调度执行成功 =====");
                map.forEach((key, value) -> {
                    log.info(key + ":" + value);
                });
            }
        }
    }


}
